package com.ivo.sys.user;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.ivo.sec.SessionLocal;
import com.ivo.util.JsonUtil;

@RestController
@RequestMapping("/sys/user")
public class UserController {
	
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private UserService userService;

    @RequestMapping(value = "/userManage")
	@ResponseBody
	public ModelAndView addDepart(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/main");
		mv.addObject("category","userManage");
		return mv;
		
	}  
	
    
    @RequestMapping(value = "/list")
    @ResponseBody
    public String list(
    		@RequestParam(value = "emp_id", required = false) String emp_id,
    		@RequestParam(value = "condition", required = false) String condition) {
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", userService.list(emp_id, condition));
//        String s = JsonUtil.toJson(returnMap);
        return JsonUtil.toJson(returnMap);
    }
    
    @RequestMapping(value = "/create")
    @ResponseBody
    public String create(HttpServletRequest request,@RequestParam(value = "user_id") String user_id,@RequestParam(value = "user_name") String user_name,@RequestParam(value = "password") String password){
    	String err_code = "";
    	String err_msg = "";

    	
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", err_code);
        returnMap.put("err_msg", err_msg);
        return JsonUtil.toJson(returnMap);
    }
    
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public String delete(HttpServletRequest request,HttpSession session,@RequestParam(value = "userids") String userids) {
    	String err_code = "";
    	String err_msg = "";

    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", err_code);
        returnMap.put("err_msg", err_msg);
        return JsonUtil.toJson(returnMap);
    }
    
    @RequestMapping(value = "/getUserRole", method = RequestMethod.POST)
    @ResponseBody
    public String getUserRole(HttpServletRequest request,HttpSession session) {
    	String err_code = "";
    	String err_msg = "";
    	String srole = "";
    	
        String user_id="sys";
        if(SessionLocal.getUser()!=null)
        user_id = SessionLocal.getUser().getUser_id();
        Role role = userService.getRoleByUser(user_id);
        if(role !=null){
    		err_code = "0";
    		err_msg = "success";
    		srole=role.getName();
        }
        
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", err_code);
        returnMap.put("err_msg", err_msg);
        returnMap.put("result",srole);
        return JsonUtil.toJson(returnMap);
    }
}
